<template>
  <div class='container'>
  <el-dialog :title="showTitle" :visible="showDialog" @close="closeShowdialog" :close-on-click-modal="false">
  <el-form :model="form" label-width="80px" :rules="rules" ref="addForm">
    <el-form-item label="学科名称" prop="subjectName">
       <el-input placeholder="请输入学科名称" v-model="form.subjectName" ></el-input>
    </el-form-item>
    <el-form-item label="是否显示" ><el-switch
      prop="isFrontDisplay"
      v-model="form.isFrontDisplay"
      active-color="#13ce66"
      inactive-color="#ff4949"
      :active-value="1"
      :inactive-value="0">
    </el-switch>
    </el-form-item>

  </el-form>
  <div slot="footer" class="dialog-footer">
    <el-button @click="closeShowdialog">取 消</el-button>
    <el-button type="primary" @click="determine">确 定</el-button>
  </div>
</el-dialog>
  </div>

</template>

<script>
import { add as addSubjects, update } from '@/api/hmmm/subjects'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    formData: {
      type: Object,
      default: () => {}
    },
    form: {
      subjectName: '',
      type: Object,
      default: () => {}
    }
  },
  computed: {
    showTitle () {
      return this.formData.id ? '修改学科' : '添加学科'
    }
  },
  data () {
    return {
      isFrontDisplay: '1',
      rules: {
        subjectName: [
          { required: true, message: '请填写学科名称', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 点击确定
    determine () {
      this.$refs.addForm.validate(async flag => {
        if (!flag) return
        if (this.form.id) {
          await update(this.form)
          this.$message.success('恭喜！编辑成功')
        } else {
          const res = await addSubjects(this.form)
          this.$message.success('恭喜！添加成功')
        }
        this.closeShowdialog()
        this.$parent.lister()
      })
    },
    closeShowdialog () {
      this.$emit('update:showDialog', false)
      this.$refs.addForm.resetFields()
      this.form = {
        subjectName: '',
        isFrontDisplay: '1'
      }
    }
  }
}
</script>

<style scoped lang='less'></style>
